package org.atf.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.atf.model.dao.ProfileDAO;
import org.atf.model.entity.Profile;

public class ApacheDashboardController {
	private  static Logger  logger = Logger.getLogger(ApacheDashboardController.class);
	public HashMap<String, String> getApacheStatusDetails(String url){
		HashMap<String,String> result= new HashMap<String,String>();
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
		logger.debug("Calling: "+url);
		HttpGet httpget = new HttpGet(url);
		logger.debug("Executing request " + httpget.getRequestLine());
		// Create a custom response handler
         ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

             public String handleResponse(
                     final HttpResponse response) throws ClientProtocolException, IOException {
                 int status = response.getStatusLine().getStatusCode();
                 if (status >= 200 && status < 300) {
                     HttpEntity entity = response.getEntity();
                     return entity != null ? EntityUtils.toString(entity) : null;
                 } else {
                     throw new ClientProtocolException("Unexpected response status: " + status);
                 }
             }

         };
         String responseBody;
     	 responseBody = httpclient.execute(httpget, responseHandler);
        
         //logger.trace(responseBody);
         String[] dts= org.apache.commons.lang3.StringUtils.substringsBetween(responseBody, "<dt>", "</dt>");
         for (String dt : dts) {
        	 //System.out.println(dt);
        	 
        	 
        	String serverVersion=org.apache.commons.lang3.StringUtils.substringAfter(dt,"Server Version:");
        	if(!serverVersion.equalsIgnoreCase("")){
        		result.put("serverVersion", serverVersion);
        		//System.out.println("dt serverVersion:" + serverVersion.trim()); // good
        	}
        	String currentTime=org.apache.commons.lang3.StringUtils.substringAfter(dt,"Current Time:");
        	if(!currentTime.equalsIgnoreCase("")){
        		result.put("currentTime", currentTime);
        		//System.out.println("dt currentTime:" + currentTime.trim()); // good
        	}
        	String restartTime=org.apache.commons.lang3.StringUtils.substringAfter(dt,"Restart Time:");
        	if(!restartTime.equalsIgnoreCase("")){
        		result.put("restartTime", restartTime);
        		//System.out.println("dt restartTime:" + restartTime.trim()); // good
        	}
        	
        	String serverUptime=org.apache.commons.lang3.StringUtils.substringAfter(dt,"Server uptime:");
        	if(!serverUptime.equalsIgnoreCase("")){
        		result.put("serverUptime", serverUptime);
        		//System.out.println("dt serverUptime:" + serverUptime.trim()); // good
        	}
        	 
 			String currentRequest=org.apache.commons.lang3.StringUtils.substringBefore(dt,"requests currently being processed,");
 			if(!currentRequest.equalsIgnoreCase(dt)){
 				//System.out.println("dt currentRequest:" + currentRequest); // good
 				result.put("currentRequests", currentRequest.trim());
 			}	
 			String idleWorkers=org.apache.commons.lang3.StringUtils.substringBetween(dt,"requests currently being processed,", "idle workers");
 			if(idleWorkers  != null){
 				result.put("idleWorkers", idleWorkers);
 				//System.out.println("dt idleWorkers:" + idleWorkers.trim());
 			}
 			
 			
 		}
        // String[] tds = StringUtils.substringsBetween(responseBody, "<td>", "</td>");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
